var t = wx.getSystemInfoSync(), e = require("../../072C1F06C63E4BDF614A7701823A7135.js").transformRpx;

Component({
    options: {
        multipleSlots: !0
    },
    relations: {
        "../recycle-item/recycle-item": {
            type: "child",
            linked: function(t) {
                if (!this._hasCheckSize) {
                    this._hasCheckSize = !0;
                    var e = this.boundingClientRect(this._pos.beginIndex);
                    if (!e) return;
                    setTimeout(function() {
                        try {
                            t.createSelectorQuery().select(".wx-recycle-item").boundingClientRect(function(t) {
                                !t || t.width === e.width && t.height === e.height || console.warn("[recycle-view] the size in <recycle-item> is not the same with param itemSize, expect {width: " + t.width + ", height: " + t.height + "} but got {width: " + e.width + ", height: " + e.height + "}");
                            }).exec();
                        } catch (t) {}
                    }, 10);
                }
            }
        }
    },
    properties: {
        debug: {
            type: Boolean,
            value: !1
        },
        scrollY: {
            type: Boolean,
            value: !0
        },
        batch: {
            type: Boolean,
            value: !1,
            public: !0,
            observer: "_recycleInnerBatchDataChanged"
        },
        batchKey: {
            type: String,
            value: "batchSetRecycleData",
            public: !0
        },
        scrollTop: {
            type: Number,
            value: 0,
            public: !0,
            observer: "_scrollTopChanged",
            observeAssignments: !0
        },
        height: {
            type: Number,
            value: t.windowHeight,
            public: !0,
            observer: "_heightChanged"
        },
        width: {
            type: Number,
            value: t.windowWidth,
            public: !0,
            observer: "_widthChanged"
        },
        upperThreshold: {
            type: Number,
            value: 50,
            public: !0
        },
        lowerThreshold: {
            type: Number,
            value: 50,
            public: !0
        },
        scrollToIndex: {
            type: Number,
            public: !0,
            value: 0,
            observer: "_scrollToIndexChanged",
            observeAssignments: !0
        },
        scrollWithAnimation: {
            type: Boolean,
            public: !0,
            value: !1
        },
        enableBackToTop: {
            type: Boolean,
            public: !0,
            value: !1
        },
        throttle: {
            type: Boolean,
            public: !0,
            value: !0
        },
        placeholderImage: {
            type: String,
            public: !0,
            value: ""
        },
        screen: {
            type: Number,
            public: !0,
            value: 4
        }
    },
    data: {
        innerBeforeHeight: 0,
        innerAfterHeight: 0,
        innerScrollTop: 0,
        innerScrollIntoView: "",
        placeholderImageStr: "",
        totalHeight: 0,
        useInPage: !1
    },
    attached: function() {
        this.data.placeholderImage && this.setData({
            placeholderImageStr: e(this.data.placeholderImage, !0)
        }), this.setItemSize({
            array: [],
            map: {},
            totalHeight: 0
        });
    },
    ready: function() {
        var t = this;
        this._initPosition(function() {
            t._isReady = !0, t._updateTimerId || t._scrollViewDidScroll({
                detail: {
                    scrollLeft: t._pos.left,
                    scrollTop: t._pos.top,
                    ignoreScroll: !0
                }
            }, !0);
        });
    },
    detached: function() {
        this.page = null, this.context && (this.context.destroy(), this.context = null);
    },
    methods: {
        _log: function() {
            var t;
            if (this.data.debug) {
                for (var e = new Date(), i = e.getHours() + ":" + e.getMinutes() + ":" + e.getSeconds() + "." + e.getMilliseconds(), o = arguments.length, r = Array(o), l = 0; l < o; l++) r[l] = arguments[l];
                Array.prototype.splice.call(r, 0, 0, i), (t = console).log.apply(t, r);
            }
        },
        _scrollToUpper: function(t) {
            this.triggerEvent("scrolltoupper", t.detail);
        },
        _scrollToLower: function(t) {
            this.triggerEvent("scrolltolower", t.detail);
        },
        _beginToScroll: function() {
            this._lastScrollTop || (this._lastScrollTop = this._pos && (this._pos.top || 0));
        },
        _clearList: function(t) {
            this.currentScrollTop = 0, this._lastScrollTop = 0;
            var e = this._pos;
            e.beginIndex = this._pos.endIndex = -1, e.afterHeight = e.minTop = e.maxTop = 0, 
            this.page._recycleViewportChange({
                detail: {
                    data: e,
                    id: this.id
                }
            }, t);
        },
        _isValid: function() {
            return this.page && this.context && this.context.isDataReady;
        },
        _scrollViewDidScroll: function(t, e) {
            if (this._isValid()) if (t.detail.ignoreScroll || this.triggerEvent("scroll", t.detail), 
            this.currentScrollTop = t.detail.scrollTop, this._pos.height && this.sizeArray.length) if (this._isScrollingWithAnimation) this._isScrollingWithAnimation = !1; else {
                var i = this._pos, o = this, r = t.detail.scrollLeft, l = t.detail.scrollTop, s = Math.abs(l - this._lastScrollTop);
                if (!e && Math.abs(l - i.top) < 1.5 * i.height) this._log("【not exceed height"); else {
                    this._lastScrollTop = l;
                    var h = this.data.screen;
                    this._log("SHOW_SCREENS", h, l), this._calcViewportIndexes(r, l, function(h, n, a, c, d) {
                        o._log("scrollDistance", s, "indexes", h, n), e || i.beginIndex !== h || i.endIndex !== n || i.minTop !== a || i.afterHeight !== c ? (o._log("【check】before setData, old pos is", i.minTop, i.maxTop, a, d), 
                        o._throttle = !1, i.left = r, i.top = l, i.beginIndex = h, i.endIndex = n, i.minTop = a, 
                        i.maxTop = d, i.afterHeight = c, i.ignoreBeginIndex = i.ignoreEndIndex = -1, o.page._recycleViewportChange({
                            detail: {
                                data: o._pos,
                                id: o.id
                            }
                        }, function() {
                            t.detail.cb && t.detail.cb();
                        })) : o._log("------------is the same beginIndex and endIndex");
                    });
                }
            } else this._clearList(t.detail.cb);
        },
        _calcViewportIndexes: function(t, e, i) {
            var o = this;
            this._getBeforeSlotHeight(function() {
                var r = o.__calcViewportIndexes(t, e), l = r.beginIndex, s = r.endIndex, h = r.minTop, n = r.afterHeight, a = r.maxTop;
                i && i(l, s, h, n, a);
            });
        },
        _getBeforeSlotHeight: function(t) {
            void 0 !== this.data.beforeSlotHeight ? t && t(this.data.beforeSlotHeight) : this.reRender(t);
        },
        _getAfterSlotHeight: function(t) {
            void 0 !== this.data.afterSlotHeight ? t && t(this.data.afterSlotHeight) : this.reRender(t);
        },
        _getIndexes: function(t, e) {
            if (t === e && 0 === e) return {
                beginIndex: -1,
                endIndex: -1
            };
            for (var i = Math.floor(t / 200), o = Math.ceil(e / 200), r = Math.floor(this.data.width / 200), l = void 0, s = void 0, h = this.sizeMap, n = i; n <= o; n++) for (var a = 0; a < r; a++) {
                var c = n + "." + a;
                if (h[c]) for (var d = 0; d < h[c].length; d++) void 0 !== l ? l > h[c][d] ? l = h[c][d] : s < h[c][d] && (s = h[c][d]) : l = s = h[c][d];
            }
            return {
                beginIndex: l,
                endIndex: s
            };
        },
        _isIndexValid: function(t, e) {
            return !(void 0 === t || -1 === t || void 0 === e || -1 === e || e >= this.sizeArray.length);
        },
        __calcViewportIndexes: function(t, e) {
            if (!this.sizeArray.length) return {};
            var i = this._pos;
            void 0 === t && (t = i.left), void 0 === e && (e = i.top);
            var o = this.data.beforeSlotHeight || 0, r = this.data.screen, l = e - i.height * r - o, s = e + i.height * r - o;
            s > this.totalHeight && (l -= s - this.totalHeight, s = this.totalHeight), l < o && (s += Math.min(o - l, this.totalHeight), 
            l = 0);
            var h = this._getIndexes(l, s), n = h.beginIndex, a = h.endIndex;
            if (a >= this.sizeArray.length && (a = this.sizeArray.length - 1), !this._isIndexValid(n, a)) return {
                beginIndex: -1,
                endIndex: -1,
                minTop: 0,
                afterHeight: 0,
                maxTop: 0
            };
            var c = this.sizeArray[a].beforeHeight + this.sizeArray[a].height;
            return {
                beginIndex: n,
                endIndex: a,
                minTop: this.sizeArray[n].beforeHeight,
                afterHeight: this.totalHeight - c,
                maxTop: s
            };
        },
        setItemSize: function(t) {
            this.sizeArray = t.array, this.sizeMap = t.map, t.totalHeight !== this.totalHeight && this.setData({
                totalHeight: t.totalHeight,
                useInPage: this.useInPage || !1
            }), this.totalHeight = t.totalHeight;
        },
        setList: function(t, e) {
            this._currentSetDataKey = t, this._currentSetDataList = e;
        },
        setPage: function(t) {
            this.page = t;
        },
        forceUpdate: function(t, e) {
            var i = this;
            if (!this._isReady) return this._updateTimerId && clearTimeout(this._updateTimerId), 
            void (this._updateTimerId = setTimeout(function() {
                i.forceUpdate(t, e);
            }, 10));
            this._updateTimerId = null;
            var o = this;
            e ? this.reRender(function() {
                o._scrollViewDidScroll({
                    detail: {
                        scrollLeft: o._pos.left,
                        scrollTop: o.currentScrollTop || o.data.scrollTop || 0,
                        ignoreScroll: !0,
                        cb: t
                    }
                }, !0);
            }) : this._scrollViewDidScroll({
                detail: {
                    scrollLeft: o._pos.left,
                    scrollTop: o.currentScrollTop || o.data.scrollTop || 0,
                    ignoreScroll: !0,
                    cb: t
                }
            }, !0);
        },
        _initPosition: function(t) {
            var e = this;
            e._pos = {
                left: e.data.scrollLeft || 0,
                top: e.data.scrollTop || 0,
                width: this.data.width,
                height: Math.max(500, this.data.height),
                direction: 0
            }, this.reRender(t);
        },
        _widthChanged: function(t) {
            return this._isReady ? (this._pos.width = t, this.forceUpdate(), t) : t;
        },
        _heightChanged: function(t) {
            return this._isReady ? (this._pos.height = Math.max(500, t), this.forceUpdate(), 
            t) : t;
        },
        reRender: function(t) {
            function e() {
                l._lastBeforeSlotHeight === o && l._lastAfterSlotHeight === r || l.setData({
                    hasBeforeSlotHeight: !0,
                    hasAfterSlotHeight: !0,
                    beforeSlotHeight: o,
                    afterSlotHeight: r
                }), l._lastBeforeSlotHeight = o, l._lastAfterSlotHeight = r, t && t();
            }
            var i = this, o = void 0, r = void 0, l = this, s = !1, h = !1;
            this.setData({
                hasBeforeSlotHeight: !1,
                hasAfterSlotHeight: !1
            }, function() {
                i.createSelectorQuery().select(".slot-before").boundingClientRect(function(t) {
                    o = t.height, s = !0, h && e && e();
                }).exec(), i.createSelectorQuery().select(".slot-after").boundingClientRect(function(t) {
                    r = t.height, h = !0, s && e && e();
                }).exec();
            });
        },
        _setInnerBeforeAndAfterHeight: function(t) {
            void 0 !== t.beforeHeight && (this._tmpBeforeHeight = t.beforeHeight), t.afterHeight && (this._tmpAfterHeight = t.afterHeight);
        },
        _recycleInnerBatchDataChanged: function(t) {
            var e = this;
            if (void 0 !== this._tmpBeforeHeight) {
                var i = {
                    innerBeforeHeight: this._tmpBeforeHeight || 0,
                    innerAfterHeight: this._tmpAfterHeight || 0
                };
                void 0 !== this._tmpInnerScrollTop && (i.innerScrollTop = this._tmpInnerScrollTop);
                var o = {}, r = !1;
                void 0 !== this._currentSetDataKey && (o[this._currentSetDataKey] = this._currentSetDataList, 
                r = !0);
                var l = this.data.scrollWithAnimation;
                r && e.page.setData(o), e.setData(i, function() {
                    e.setData({
                        scrollWithAnimation: l
                    }), "function" == typeof t && t();
                }), delete this._currentSetDataKey, delete this._currentSetDataList, this._tmpBeforeHeight = void 0, 
                this._tmpAfterHeight = void 0, this._tmpInnerScrollTop = void 0;
            }
        },
        _renderByScrollTop: function(t) {
            this._scrollViewDidScroll({
                detail: {
                    scrollLeft: this._pos.scrollLeft,
                    scrollTop: t,
                    ignoreScroll: !0
                }
            }, !0), this.data.scrollWithAnimation && (this._isScrollingWithAnimation = !0), 
            this.setData({
                innerScrollTop: t
            });
        },
        _scrollTopChanged: function(t, e) {
            var i = this;
            return this._isInitScrollTop || 0 !== t ? (this.currentScrollTop = t, this._isReady ? (this._isInitScrollTop = !0, 
            this._scrollTopTimerId = null, void 0 === this._lastScrollTop && (this._lastScrollTop = this.data.scrollTop), 
            Math.abs(t - this._lastScrollTop) < this._pos.height ? (this.setData({
                innerScrollTop: t
            }), t) : (this._isScrollTopChanged ? this._renderByScrollTop(t) : setTimeout(function() {
                i._isScrollTopChanged = !0, i._renderByScrollTop(t);
            }, 10), t)) : (this._scrollTopTimerId && clearTimeout(this._scrollTopTimerId), this._scrollTopTimerId = setTimeout(function() {
                i._scrollTopChanged(t, e);
            }, 10), t)) : (this._isInitScrollTop = !0, t);
        },
        _scrollToIndexChanged: function(t, e) {
            var i = this;
            if (!this._isInitScrollToIndex && 0 === t) return this._isInitScrollToIndex = !0, 
            t;
            if (!this._isReady) return this._scrollToIndexTimerId && clearTimeout(this._scrollToIndexTimerId), 
            this._scrollToIndexTimerId = setTimeout(function() {
                i._scrollToIndexChanged(t, e);
            }, 10), t;
            this._isInitScrollToIndex = !0, this._scrollToIndexTimerId = null, void 0 === this._lastScrollTop && (this._lastScrollTop = this.data.scrollTop);
            var o = this.boundingClientRect(t);
            if (!o) return t;
            var r = o.top + (this.data.beforeSlotHeight || 0);
            return this.currentScrollTop = r, Math.abs(r - this._lastScrollTop) < this._pos.height ? (this.setData({
                innerScrollTop: r
            }), t) : (this._isScrollToIndexChanged ? this._renderByScrollTop(r) : setTimeout(function() {
                i._isScrollToIndexChanged = !0, i._renderByScrollTop(r);
            }, 10), t);
        },
        boundingClientRect: function(t) {
            return t < 0 || t >= this.sizeArray.length ? null : {
                left: 0,
                top: this.sizeArray[t].beforeHeight,
                width: this.sizeArray[t].width,
                height: this.sizeArray[t].height
            };
        },
        getIndexesInViewport: function(t) {
            t || (t = 1);
            var e = this.currentScrollTop, i = e + t;
            i < 0 && (i = 0);
            var o = e + this.data.height - t;
            o > this.totalHeight && (o = this.totalHeight);
            for (var r = [], l = 0; l < this.sizeArray.length && (this.sizeArray[l].beforeHeight + this.sizeArray[l].height >= i && this.sizeArray[l].beforeHeight <= o && r.push(l), 
            !(this.sizeArray[l].beforeHeight > o)); l++) ;
            return r;
        },
        getTotalHeight: function() {
            return this.totalHeight;
        },
        setUseInPage: function(t) {
            this.useInPage = t;
        },
        setPlaceholderImage: function(t, e) {
            var i = [ "data:image/svg+xml,%3Csvg height='" + e.height + "' width='" + e.width + "' xmlns='http://www.w3.org/2000/svg'%3E" ];
            t.forEach(function(t) {
                i.push("%3Crect width='" + t.width + "' x='" + t.left + "' height='" + t.height + "' y='" + t.top + "' style='fill:rgb(204,204,204);' /%3E");
            }), i.push("%3C/svg%3E"), this.setData({
                placeholderImageStr: i.join("")
            });
        }
    }
});